﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO;

namespace Client
{
    public partial class CreateProjectForm : System.Windows.Forms.Form
    {
        public CreateProjectForm()
        {
            InitializeComponent();
        }

        public CreateProjectForm(string directoryAdr)
        {
            InitializeComponent();
            txtDirectory.Text = directoryAdr;
        }

        // 새로운 프로젝트를 등록
        private void btnCreate_Click(object sender, EventArgs e)
        {          
            // 프로젝트명          // txtProjName.Text 
            // 관리자 아이디       // MainForm.userData.UserID
            // 멤버 아이디's       // list<string> 
            // 서버로 송신.
     
            List<CCL.SocketMessage.Request.RegistProjectMemberList.Member> mbList = new List<CCL.SocketMessage.Request.RegistProjectMemberList.Member>();
            CheckedListBox.ObjectCollection oc = memberList.Items;
            foreach (object item in oc)
            {
                CCL.SocketMessage.Request.RegistProjectMemberList.Member member = new CCL.SocketMessage.Request.RegistProjectMemberList.Member((string)item, CCL.SocketMessage.Constant.Permission.All);
                mbList.Add(member);
            }

            CCL.SocketMessage.Request.RegistProject body = new CCL.SocketMessage.Request.RegistProject(txtProjName.Text, MainForm.userData.UserID);
            CCL.SocketMessage.Message msg = CCL.SocketMessage.Message.MessageRequestBuilder(body);

            CCL.SocketMessage.Message recvMsg = MainForm.connector.Execute(msg.GetByte());
            CCL.SocketMessage.Response.RegistProject respBody = (CCL.SocketMessage.Response.RegistProject)recvMsg.Body;
            if (respBody.Result == CCL.SocketMessage.Constant.Result.Fail)
            {
                MessageBox.Show(CCL.Utility.StringUtility.ConvertToStr(respBody.ErrorMessage));
            }
            else
            {
                CCL.SocketMessage.Request.RegistProjectMemberList mlBody = new CCL.SocketMessage.Request.RegistProjectMemberList(respBody.Project.ProjectID, mbList);
                CCL.SocketMessage.Message memberMsg = CCL.SocketMessage.Message.MessageRequestBuilder(mlBody);

                CCL.SocketMessage.Message memberRecvMsg = MainForm.connector.Execute(memberMsg.GetByte());
                CCL.SocketMessage.Response.RegistProjectMemberList respMemberBody = (CCL.SocketMessage.Response.RegistProjectMemberList)memberRecvMsg.Body;

                if (respMemberBody.Result != CCL.SocketMessage.Constant.Result.Success)
                {
                    MessageBox.Show(CCL.Utility.StringUtility.ConvertToStr(respMemberBody.ErrorMessage));
                    return;
                }
                string[] data = {respBody.Project.ProjectID.ToString(), "1", System.DateTime.Now.ToString("yyyyMMddHHmmss")};
                TxtReader.CreateDataFile(txtDirectory.Text);
                TxtReader.WriteDataFile(txtDirectory.Text, data);
                TxtReader.WriteSystemFile(respBody.Project.ProjectID.ToString(), txtDirectory.Text);

                MessageBox.Show("정상적으로 프로젝트가 등록되었습니다.");
            }

            this.Close();
        }

        // member 목록에서 삭제
        private void btnRemove_Click(object sender, EventArgs e)
        {
            // Check 한 목록들을 삭제하는 경우
            CheckedListBox.CheckedItemCollection cc = memberList.CheckedItems;
            foreach (object item in cc)
            {
                memberList.Items.Remove(item);
                break;
            }
            
            // Select 한 목록을 삭제하는 경우
            ListBox.SelectedIndexCollection sc = memberList.SelectedIndices;
            foreach (int index in sc)
            {
                memberList.Items.Remove(memberList.Items[index]);
            }
            
        }

        // member 목록에 추가
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (txtUserName.Text != "") 
            {
                // Email 주소를 입력하였는지 확인
                if (txtUserName.Text.Contains('@') == true)
                {
                    // Server 에 Email 주소를 송신하여 회원이 존재하는지 확인.
                    

                    // 멤버리스트에 추가
                    memberList.Items.Add(txtUserName.Text);
                }
                else
                {
                    MessageBox.Show("이메일 주소가 아닙니다.");
                }
                
            }
            else
            {
                // txtUserName 에 데이터가 들어있지 않을 경우
                MessageBox.Show("이메일 주소를 입력하세요.");
            }
        }

        // directroy searcher 실행
        // 프로젝트 디렉토리 절대 경로를 저장.
        private void btnDirectorySearch_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                txtDirectory.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
